package org.example.springaop.proxy;

import lombok.extern.slf4j.Slf4j;
import org.example.springaop.house.IHouse;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 九转苍翎
 * Date: 2025/8/2
 * Time: 12:56
 */
@Slf4j
public class CGLibMethodInterceptor implements MethodInterceptor {

    private final Object realHouse;

    public CGLibMethodInterceptor(Object realHouse) {
        this.realHouse = realHouse;
    }

    @Override
    //Object obj:动态生成的代理对象实例,Method method:当前被拦截的目标方法对象,Object[] args:方法调用时传入的参数数组,MethodProxy proxy:方法代理对象
    public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
        log.info("开始代理");
        IHouse result = (IHouse)method.invoke(realHouse, args);
        log.info("结束代理");
        return result;
    }
}
